package com.example.a1.fannong.core.manager.user;

import android.os.Handler;
import android.os.Message;

import java.util.ArrayList;
import java.util.List;

/**
 * 验证码间隔控制
 *   Created by nishuhang on 16/12/30.
 */
public class VerifyCodeController {
    private long lastSendTime = 0;
    private int maxInterval; // 间隔时间  单位 毫秒

    private long currentInterval = 0;

    private List<OnVerifyCodeIntervalChangeListener> onVerifyCodeIntervalChangeListeners = new ArrayList<>();
    private Handler handler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message message) {
            if (message.what == 0) {
                long interval = Math.abs(System.currentTimeMillis() - lastSendTime);
                if (interval >= maxInterval) {
                    // 间隔时间到了
                    currentInterval = 0;
                    notifyIntervalChange();
                } else {
                    currentInterval = maxInterval - interval;
                    notifyIntervalChange();
                    handler.sendEmptyMessageDelayed(0, 1000);
                }
                return true;
            } else {
                return false;
            }
        }
    });

    /**
     * @param maxInterval 间隔时间，单位 毫秒
     */
    protected VerifyCodeController(int maxInterval) {
        this.maxInterval = maxInterval;
    }

    public void addOnVerifyCodeIntervalChangeListener(OnVerifyCodeIntervalChangeListener listener) {
        onVerifyCodeIntervalChangeListeners.add(listener);
    }

    public void removeOnVerifyCodeIntervalChangeListener(OnVerifyCodeIntervalChangeListener listener) {
        onVerifyCodeIntervalChangeListeners.remove(listener);
    }

    private void notifyIntervalChange() {
        List<OnVerifyCodeIntervalChangeListener> listeners = new ArrayList<>(onVerifyCodeIntervalChangeListeners);
        for (OnVerifyCodeIntervalChangeListener listener : listeners) {
            listener.onVerifyCodeIntervalChange(currentInterval);
        }
    }

    /**
     * 获取最大间隔时间
     */
    public int getMaxInterval() {
        return maxInterval;
    }

    /**
     * 当前间隔时间 单位：毫秒
     */
    public long getCurrentInterval() {
        return currentInterval;
    }

//    /**
//     * 发送验证码
//     *
//     * @param phoneNum         手机号
//     * @param onResultListener 结果监听
//     */
//    public void getVerifyCode(String phoneNum, int type, long uid, final OnResultListener<Void> onResultListener) {
//        long interval = Math.abs(System.currentTimeMillis() - lastSendTime);
//        if (interval < maxInterval) {
//            onResultListener.onResult(OnResultListener.RESULT_FAILED, "间隔时间太短", null);
//            return;
//        }
//
//        GetVerifyCodeRequester requester = new GetVerifyCodeRequester(new OnResultListener<Void>() {
//            @Override
//            public void onResult(int result, String msg, Void aVoid) {
//                if (result == RESULT_SUCCESS) {
//                    // 验证码发送成功，开始计时
//                    lastSendTime = System.currentTimeMillis();
//                    currentInterval = maxInterval;
//                    handler.sendEmptyMessageDelayed(0, 0);
//                }
//                onResultListener.onResult(result, msg, aVoid);
//            }
//        });
//        requester.phone = phoneNum;
//        requester.type = type;
//        requester.uid = uid;
//        requester.doGet(500);
//    }

    public interface OnVerifyCodeIntervalChangeListener {
        void onVerifyCodeIntervalChange(long interval);
    }
}
